package com.example.abe.diandi.trends.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.BindView;
import com.example.abe.diandi.R;
import com.example.abe.diandi.base.BaseFragment;
import com.example.abe.diandi.trends.model.ImageTextModel;
import com.example.abe.diandi.trends.model.TrendModel;
import com.example.abe.diandi.trends.view.TrendsShowImageView;

import java.util.ArrayList;

/**
 * Created by abe on 17/1/12.
 */
public class TrendsShowImageFragment extends BaseFragment {

    private static final String ARG_LIST_IMAGE_TEXT_MODEL = "arguments_list_image_text_model";
    private static final String ARG_NOTE_MODEL_TYPE = "arguments_note_model_type";
    private static final String IMAGE_BROWSE_DIALOG_FRAGMENT = "image_browse_dialog_fragment";

    @BindView(R.id.frag_trends_show_image_ll_content)
    protected LinearLayout mLlContent;

    private ArrayList<ImageTextModel> mList;

    private int mNoteType;

    public static TrendsShowImageFragment instance(ArrayList<ImageTextModel> list, int noteType) {
        TrendsShowImageFragment fragment = new TrendsShowImageFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(ARG_LIST_IMAGE_TEXT_MODEL, list);
        bundle.putInt(ARG_NOTE_MODEL_TYPE, noteType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayoutID() {
        return R.layout.fragment_trends_show_image;
    }

    @Override
    public void initData() {
        mList = getArguments().getParcelableArrayList(ARG_LIST_IMAGE_TEXT_MODEL);
        mNoteType = getArguments().getInt(ARG_NOTE_MODEL_TYPE, TrendModel.TYPE_IMAGE);
    }

    @Override
    public void initView() {
        for (ImageTextModel model: mList){
            TrendsShowImageView view = new TrendsShowImageView(getActivity(), model.getId());
            view.setImageByNetwork(model.getImage());
            view.setContent(model.getContent());
            view.setListener(onImageViewClickListener);
            if (mNoteType == TrendModel.TYPE_IMAGE){
                view.setTextContentVisibility(false);
            }else if (mNoteType == TrendModel.TYPE_IMAGE_TEXT){
                view.setTextContentVisibility(true);
            }
            mLlContent.addView(view);
        }
    }

    TrendsShowImageView.OnImageViewClickListener onImageViewClickListener = new TrendsShowImageView.OnImageViewClickListener() {
        @Override
        public void onImageViewClick(ImageView iv, ImageTextModel model) {
            FragmentTransaction ft = getChildFragmentManager().beginTransaction();
            Fragment prev = getChildFragmentManager().findFragmentByTag(IMAGE_BROWSE_DIALOG_FRAGMENT);
            if (prev != null) {
                ft.remove(prev);
            }
            ImageBrowseDialogFragment fragment = ImageBrowseDialogFragment.newInstance(mList, model.getId());
            fragment.show(ft, IMAGE_BROWSE_DIALOG_FRAGMENT);
        }
    };
}
